// pages/orderInfo/index.js
import Dialog from '@vant/weapp/dialog/dialog';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    id: '',
    orderInfo: {},
    imgUrl: app.globalData.imgUrl,
    userid: wx.getStorageSync('userId')
  },
  // 获取列表
  getInfo() {
    let that = this
    // 获取采购订单详情
    wx.$req({
      url: '/wms/orderPurchase/' + that.data.id,
      loadingText: '加载中...'
    }).then(res => {
      let info = res.data

      if (info.picture) {
        if (info.picture.includes(',')) {
          let list = info.picture.split(',')
          let ss = []
          list.forEach(element => {
            element = this.data.imgUrl + element
            ss.push(element)
          });
          info.imgList = ss
        } else {
          info.imgList = [this.data.imgUrl + info.picture]
        }
      }
      that.setData({
        orderInfo: info
      })
    })
  },
  // 图片预览
  preview(e) {
    wx.previewImage({
      urls: this.data.orderInfo.imgList,
      current: e.currentTarget.dataset.url,
    })
  },
  showImg(e) {
    // this.setData({show:true})
    wx.previewImage({
      urls: [e.currentTarget.dataset.url],
      current: e.currentTarget.dataset.url,
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options.id
    this.setData({
      id: id
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getInfo()
  },
  // 接单
  submit() {
    Dialog.confirm({
        title: '接单',
        message: '是否确认接单？',
      })
      .then(() => {
        let that = this
        wx.$req({
          url: '/wms/orderSail/receiving/' + that.data.id,
          loadingText: '加载中...',
          method: "Put"
        }).then(res => {
          if (res.data.code == 500) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '接单成功',
              icon: 'success'
            })
            wx.navigateBack()
          }
        })
      })
      .catch(() => {
        // on cancel
      })
  },
  // 入库
  inStore() {
    wx.navigateTo({
      url: `/pages/productOrderInfo/add/index?id=${this.data.id}&productId=${this.data.orderInfo.productId}`,
    })
    return
    Dialog.confirm({
        title: '入库',
        message: '是否确认入库？',
      })
      .then(() => {
        let that = this
        wx.$req({
          url: '/wms/orderPurchase/in',
          loadingText: '加载中...',
          data: {
            id: this.data.id
          },
          method: "Put"
        }).then(res => {
          if (res.data.code == 500) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '入库成功',
              icon: 'success'
            })
            wx.navigateBack()
          }
        })
      })
      .catch(() => {
        // on cancel
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})